package com.metaera.app.ui.popup;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.CategoryPresenter;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.HomeNewsTypeResponse;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.abs.IPagerNavigator;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

/**
 * 首页的分类
 */
public class HomeCategoryPopup extends BottomPopupView implements INetworkRequestView<List<HomeNewsTypeResponse>> {

    private RecyclerView recyclerView;

    private ViewPager viewPager;

    public HomeCategoryPopup(@NonNull Context context) {
        super(context);
    }

    public HomeCategoryPopup(@NonNull Context context, ViewPager viewPager) {
        super(context);
        this.viewPager = viewPager;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_home_category;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        recyclerView = findViewById(R.id.recycler_view);

        CategoryPresenter presenter = new CategoryPresenter();
        presenter.setModelAndView(this);
        presenter.home();

        // 关闭
        findViewById(R.id.button_close).setOnClickListener(v -> {
            // 更改位置
            dismiss();
        });

    }

    @Override
    public void successful(List<HomeNewsTypeResponse> data) {
        Resources resources = getContext().getResources();
        data.add(0, new HomeNewsTypeResponse(resources.getString(R.string.follow)));
        data.add(1, new HomeNewsTypeResponse(resources.getString(R.string.top_news)));
        data.add(2, new HomeNewsTypeResponse(resources.getString(R.string.home_activity)));

        BaseQuickAdapter<HomeNewsTypeResponse, BaseViewHolder> adapter =
                new BaseQuickAdapter<HomeNewsTypeResponse, BaseViewHolder>(R.layout.item_popup_home_category, data) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder holder, HomeNewsTypeResponse item) {
                        holder.setText(R.id.tv_name, item.getName());
                    }
                };

        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener((_a, view, position) -> {
            viewPager.setCurrentItem(position);
            dismiss();
        });
    }

}
